<template>
  <div class="department-container">
    <div class="app-container">
      <!-- 头部 -->
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAddDepartment('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <!-- 树状图 -->
      <el-tree
        :data="list"
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
      >
        <template v-slot="scope">
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{ scope.data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="right">
                <!-- 两个内容 -->
                <el-col>{{ scope.data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="doAddDepartment(scope.data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="doEditDepartment(scope.data.id)">编辑子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="doDeleteDepartment(scope.data.id)">删除子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>

      </el-tree>
      <!-- 新增部门弹窗 -->
      <el-dialog
        :visible.sync="isShow"
        :title="isEdit?'编辑部门':'新增部门'"
        @close="closeDialog"
      >
        <dialogDepartment
          :id="id"
          ref="dialogRef"
          :edit="isEdit"
          @success="addSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment, deleteDepartment } from '@/api/department'
import { array2tree } from '@/utils'
import dialogDepartment from './dialogDepartment.vue'
export default {
  components: { dialogDepartment },
  data() {
    return {
      list: [],
      id: '',
      isShow: false,
      isEdit: false
    }
  },
  created() {
    this.doDepartment()
  },
  methods: {
    async doDepartment() {
      const res = await getDepartment()
      res.data.data.depts.shift()
      this.list = array2tree(res.data.data.depts, '')
    },
    doAddDepartment(id) {
      this.isShow = true
      this.id = id
      this.isEdit = false
    },
    // 部门新增成功后，子组件传回函数
    addSuccess() {
      this.isShow = false
      this.doDepartment()
    },
    // 删除部门
    doDeleteDepartment(id) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteDepartment(id)
        this.doDepartment()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑部门
    doEditDepartment(id) {
      this.isEdit = true
      this.isShow = true
      this.id = id
      // 调用子组件的函数
      this.$nextTick(() => {
        this.$refs.dialogRef.getDepartmentInfo()
      })
    },
    // 关闭弹窗触发的函数
    closeDialog() {
      this.$refs.dialogRef.clear()
    }
  }
}
</script>

<style>

</style>
